package cn.afactory.tygz.ui.base.views.subject;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.afactory.tygz.R;
import cn.afactory.tygz.model.SnsSubjectDetailList;
import cn.afactory.tygz.ui.base.utils.sns.ViewTools;


/**
 * @author xiezhidong251@jk.cn
 * @Description
 * @date 2015/7/13.
 */
public class SubjectHeaderView extends LinearLayout {
    private Context mContext;
    private View mView;
    private OnItemClickListener mItemClickListener;
    public SnsSubjectDetailList mItem = null;

    public SubjectHeaderView(Context context) {
        super(context);
        initView();
    }

    public SubjectHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SubjectHeaderView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        mView = inflater.inflate(R.layout.health_circle_headerview, this, true);
//        findview(mView);
        mView.setVisibility(View.GONE);
        mContext = getContext();
    }

    /** 设置热门帖子小红点
     * @param b
     */
    public void setRedPointSubject(boolean b) {
        final ViewHolder viewHolder = getViewHolder(mView);
        if(b){
//            ViewTools.setTextRightIcon(viewHolder.tvSubject, R.drawable.unread_dot);
        } else {
            ViewTools.setTextRightIcon(viewHolder.tvSubject, 0);
        }
    }

    /** 设置#热门话题#小红点
     * @param b
     */
    public void setRedPointTopic(boolean b) {
        final ViewHolder viewHolder = getViewHolder(mView);
        if(b){
//            ViewTools.setTextRightIcon(viewHolder.tvTopic, R.drawable.unread_dot);
        } else {
            ViewTools.setTextRightIcon(viewHolder.tvTopic, 0);
        }
    }

    public interface OnItemClickListener {
        public void onClickSubject(long subjectId);
        public void onClickHotTopic();
        public void onClickHotSubject();
    }

    public void setItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    private ViewHolder getViewHolder(View view){
        ViewHolder viewHolder = new ViewHolder();
        viewHolder.tvTopSubject = (TextView) view.findViewById(R.id.tv_hot_top_subject);
        viewHolder.tvSubject = (TextView) view.findViewById(R.id.tv_hot_subject);
        viewHolder.tvTopic = (TextView) view.findViewById(R.id.tv_hot_topic);
        viewHolder.llSubject = (LinearLayout) view.findViewById(R.id.ll_hot_subject);
        viewHolder.llTopic = (LinearLayout) view.findViewById(R.id.ll_hot_topic);
        viewHolder.llTopSubject = (LinearLayout) view.findViewById(R.id.ll_top);
        return viewHolder;
    }

    private static class ViewHolder {
        TextView tvTopSubject;
        TextView tvSubject;
        TextView tvTopic;

        LinearLayout llSubject;
        LinearLayout llTopic;
        LinearLayout llTopSubject;

    }

    public void setData(final SnsSubjectDetailList item) {
        mItem = item;
        if (item == null) {
            return;
        }
        mView.setVisibility(View.VISIBLE);
        final ViewHolder viewHolder = getViewHolder(mView);

        viewHolder.llSubject.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onClickHotSubject();
            }
        });

        viewHolder.llTopic.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mItemClickListener.onClickHotTopic();
            }
        });

//        if(item.topSubject != null) {
//                if (!TextUtils.isEmpty(item.topSubject.content)) {
//                    viewHolder.llTopSubject.setVisibility(View.VISIBLE);
//                    viewHolder.tvTopSubject.setText(item.topSubject.content);
//                    viewHolder.llTopSubject.setOnClickListener(new OnClickListener() {
//                        @Override
//                        public void onClick(View v) {
//                            mItemClickListener.onClickSubject(item.topSubject.subjectId);
//                        }
//                    });
//                    return;
//                }
//        }
        viewHolder.llTopSubject.setVisibility(View.GONE);
    }
}
